//
//  BalanceView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/29.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BalanceView.h"
#import "SYRequestAPI+Wode.h"

@implementation BalanceView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    alertViewAlert = NO;
    pageSize = 30;
    pageIndex = 1;
    type = @"";
    tittleArray = @[@"学生补课消课",@"学生补课消课",@"购买资料",@"支付宝充值"];
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.navView];
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_navView.mas_bottom).offset(30);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [_tableList headerRefreshBlock:^{
        self->pageIndex = 1;
        [self->_dataArray removeAllObjects];
        [self getBalanceDetailListRequest];
    }];
    [_tableList fooderRefreshBlock:^{
        [self getBalanceDetailListRequest];
    }];
    
}
- (void)dismissView{
    if (alertViewAlert == YES) {
        [_alertView removeFromSuperview];
        _alertView = nil;
        alertViewAlert = NO;
    }
}
- (BanlanceNavView *)navView{
    if (!_navView) {
        BanlanceNavView *topView = [[BanlanceNavView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        topView.backgroundColor = [UIColor whiteColor];
        topView.delegate = self;
        topView.titleLabe.text = @"余额";
        [topView initOption];
        _navView = topView;
    }
    return _navView;
}
- (void)backClick{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.frame.size.width-200, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[BalanceViewCell class] forCellReuseIdentifier:@"BalanceViewCell"];
        tableView.scrollEnabled = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BalanceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BalanceViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[BalanceViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BalanceViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    cell.leftLabel.text = tittleArray[indexPath.section];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, self.frame.size.width, 20);
    return view;
}
- (void)submiteClick{
//    if (!_alertView) {
    [self.alertView removeFromSuperview];
    
    self->alertViewAlert = NO;
    
        _alertView  = [[MyBalanceAlertView alloc]initWithFrame:CGRectMake(self.frame.size.width-140, 44, 120, 227)];
        _alertView.layer.shadowOffset = CGSizeMake(0,0);
        // 设置阴影透明度
        _alertView.layer.shadowOpacity = 0.5;
        // 设置阴影半径
        _alertView.layer.shadowRadius = 5;
        _alertView.clipsToBounds = NO;
        [self addSubview:self.alertView];
        alertViewAlert = YES;
         __weak typeof(self) weakSelf = self;
        _alertView.balenceBlock = ^(NSString * _Nonnull type) {
             __strong __typeof(weakSelf)strongSelf = weakSelf;
            if (strongSelf) {
                self->type = type;
                [strongSelf getBalanceDetailListRequest];
                [weakSelf.navView.allButton setTitle:getNameOfTepy(type) forState:UIControlStateNormal];
            }
            [weakSelf.alertView removeFromSuperview];
//             weakSelf.alertView = nil;
            self->alertViewAlert = NO;
        };
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
//        [self addGestureRecognizer:tap];
//    }else{
//        [_alertView removeFromSuperview];
//        _alertView = nil;
//        alertViewAlert = NO;
//    }
}
OS_INLINE NSString *getNameOfTepy(NSString *type){
    if ([type isEqualToString:@"-1"]) {
        return @"全部";
    }else if ([type isEqualToString:@"0"]){
        return @"收入";
    }else if ([type isEqualToString:@"1"]){
        return @"支出";
    }else if ([type isEqualToString:@"2"]){
        return @"提现";
    }else{
        return @"充值";
    }
}
#pragma mark - 得到资金明细
- (void)getBalanceDetailListRequest{
    NSDictionary *parama = @{@"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSize],
                             @"inout":@"",
                             @"beginDate":@" ",
                             @"endDate":@" "};
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getSafeServiceFeeLog:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"得到资金明细列表 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            BalanceListModel *model = [BalanceListModel yy_modelWithJSON:dic];
            [self->_dataArray addObject:model];
        }
        [MBProgressHUD mj_hideHUDForView:self];
        [self.tableList reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
    
}
@end
